package com.example.smartcity.ui.smartCity.activitytModule

import android.app.Activity
import android.content.ContentValues
import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.media.MediaActionSound
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.provider.MediaStore
import android.view.MenuItem
import androidx.core.content.FileProvider
import androidx.core.graphics.get
import androidx.core.os.bundleOf
import com.example.smartcity.R
import com.example.smartcity.databinding.ActivityCameraBinding
import com.example.smartcity.logic.dao.SspDataSqlite
import com.example.smartcity.logic.utils.Toast.Companion.showToast
import java.io.File

class CameraActivity : AppCompatActivity() {

    private lateinit var bitmap: Bitmap
    private lateinit var mBinding: ActivityCameraBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = ActivityCameraBinding.inflate(layoutInflater)
        setContentView(mBinding.root)
        title = "随手拍详细"
        supportActionBar?.setDisplayHomeAsUpEnabled(true)

        // 启动相机
        startActivityForResult(Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100)


        // 点击提交
        val dbHelper = SspDataSqlite(this, "ssp.db", 5)
        // 创建数据库
        val db = dbHelper.writableDatabase

        mBinding.CameraOk.setOnClickListener {
            if (mBinding.CameraTitle.text.isEmpty() || mBinding.CameraContent.text.isEmpty()){
                "标题或内容不能为空！".showToast()
            }else {
                // 添加数据
                val value1 = ContentValues().apply {
                    put("title", mBinding.CameraTitle.text.toString())
                    put("content", mBinding.CameraContent.text.toString())
                }
                db.insert("ssp", null, value1)
                finish()
                "提交成功！".showToast()
            }
        }


    }





    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 100){
            val bundle = data?.extras
            bitmap = bundle?.get("data") as Bitmap
            mBinding.SspImageView.setImageBitmap(bitmap)
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        this@CameraActivity.finish()
        return super.onOptionsItemSelected(item)
    }
}